##############################################################################
source ( "{$app_root}/cgi-lib/R/ep.io.R" );
source ( "{$app_root}/cgi-lib/R/SigAlgFuncs.R" );
source ( "{$app_root}/cgi-lib/R/recurrentFunctions.R" );

##############################################################################
# Parameter template
##############################################################################
src="{$src}";
condThreshold="{$condThreshold}"
geneThreshold="{$geneThreshold}"
nDerivSets="{$nDerivSets}";
fractionFromInput="{$fractionFromInput}";
fractionFromGenome="{$fractionFromGenome}";
minRecc="{$minRecc}";

mode(condThreshold)="numeric";
mode(geneThreshold)="numeric";
mode(nDerivSets)="numeric";
mode(fractionFromInput)="numeric";
mode(fractionFromGenome)="numeric";
mode(minRecc)="numeric";

geneList={$geneList};
filename="{$filename}"
outputdir="{$outputdir}"

outbase=paste(outputdir, "/", "SigAlg", sep="");
# outbase=paste(outputdir,"/","tst.txt",sep="");

#-----------------------------------------------------------------------------
# read data matrix and calculate the two normalized matrices
#-----------------------------------------------------------------------------
dataset<-ep.readBin(src);
nd1<-normalize(dataset);
nd2<-t(normalize(t(dataset)));

#-----------------------------------------------------------------------------
# initialize the input gene set vector. 0: gene absent, 1: gene included
#-----------------------------------------------------------------------------
nGenesGenome<-dim(nd1)[1];
v<-rep(0,nGenesGenome);
dim(v)=c(nGenesGenome,1);
v[geneList,1]<-1;

#-----------------------------------------------------------------------------
# apply algorithm
#-----------------------------------------------------------------------------
fileName <- paste ( outbase, filename, ".png", sep="" );
print(paste("ReccImage: ", filename, ".png", sep=""));
out<-appRecSigAlg(v,nDerivSets,fractionFromInput,fractionFromGenome,minRecc,condThreshold,geneThreshold,nd1,nd2,fileName);


#-----------------------------------------------------------------------------
# sort output genes and conditions by score
#-----------------------------------------------------------------------------
genes<-which(out[[1]]!=0);
geneScores<-out[[1]][genes];
s<-sort(geneScores,decreasing=TRUE,index.return=TRUE);
genes<-genes[s[[2]]];
geneScores<-geneScores[s[[2]]];
geneScores<-signif(100*geneScores/max(geneScores),4);

conditions<-which(out[[2]]!=0);
condScores<-out[[2]][conditions];
s<-sort(abs(condScores),decreasing=TRUE,index.return=TRUE);
conditions<-conditions[s[[2]]];
condScores<-condScores[s[[2]]];
condScores<-signif(100*condScores/max(abs(condScores)),4);

#-----------------------------------------------------------------------------
# write out the results to a file and return the file names 
#-----------------------------------------------------------------------------
# write genes to file
fileName <- paste ( outbase, filename, ".genes", sep="" );
write.table (genes, quote=F, row.names=F, col.names=F, file=fileName );
genesFileName = fileName;

# write gene scores to file
fileName <- paste ( outbase, filename, ".genescores", sep="" );
write.table (geneScores,quote=F,row.names=F, col.names=F, file=fileName);
geneScoresFileName = fileName;

# write conditions to file
fileName <- paste ( outbase, filename, ".conditions", sep="" );
write.table (conditions, quote=F, row.names=F, col.names=F, file=fileName );
conditionsFileName = fileName;

# write condition scores to file
fileName <- paste ( outbase, filename, ".condscores", sep="" );
write.table (condScores,quote=F,row.names=F, col.names=F, file=fileName);
condScoresFileName = fileName;

return ( c( genesFileName, geneScoresFileName, conditionsFileName, condScoresFileName ) );
